package org.example.java.base;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class DateHandler implements MetaObjectHandler {

//    @Value("${spring.jackson.date-format}")
    private final String dateFormat = "yyyy-MM-dd HH:mm:ss";

    String date = new SimpleDateFormat(dateFormat).format(new Date());

    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName("createdAt", date, metaObject);
        setFieldValByName("lastModifiedAt", date, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("lastModifiedAt", date, metaObject);
    }
}
